# -*- coding = utf-8 -*-
# @Time : 2024/6/9 18:10
# @Author : 0505200213石浪
# @File : manager.py
# @Software : PyCharm

from flask import request, redirect, url_for, render_template, jsonify, session

import utils
from . import bp
from db_operations import Database
import datetime

db = Database()


# 管理员发送私信逻辑
@bp.route('/send_message', methods=['post'])
def send_message():
    data = request.json
    sender_username = '系统发送'
    receiver_username = data.get('receiver')
    message = data.get('message')
    sent_at = datetime.datetime.now()
    sql = "INSERT INTO movieFriends.private_messages (sender_username, receiver_username, message, sent_at) VALUES (" \
          "%s, %s, %s, %s) "
    db.execute(sql, (sender_username, receiver_username, message, sent_at))
    return jsonify({'message': '发送私信成功'})


# 管理电影页面
@bp.route('/manage_movie')
def manage_movie():
    utils.get_movies()
    return render_template("manage_movie.html", movies=utils.moviesData)


#  管理用户页面
@bp.route('/manage_user')
def manage_user():
    sql = "SELECT * FROM movieFriends.user"
    user_data = db.execute(sql)
    return render_template("manage_user.html", users=user_data)


# 添加电影逻辑
@bp.route('/add_movie', methods=['GET', 'POST'])
def add_movie():
    if request.method == 'POST':
        link = request.form['link']
        photo = request.form['photo']
        cName = request.form['cName']
        introduce = request.form['introduce']
        grade = request.form['grade']
        evaluatorsNum = request.form['evaluatorsNum']
        ink = request.form['ink']
        sql = "INSERT INTO movieFriends.movies (link, photo, cName, introduce, grade, evaluatorsNum, ink) VALUES (" \
              "%s, %s, %s, %s, %s, %s, %s) "
        print(sql, (link, photo, cName, introduce, grade, evaluatorsNum, ink))
        db.execute(sql, (link, photo, cName, introduce, grade, evaluatorsNum, ink))
        return redirect(url_for('main.manage_movie'))
    return render_template('add_movie.html')


# 修改电影逻辑
@bp.route('/edit_movie/<int:movie_id>', methods=['GET', 'POST'])
def edit_movie(movie_id):
    print('进入')
    if request.method == 'GET':
        sql = "SELECT * FROM movieFriends.movies WHERE id = %s"
        movie = db.execute(sql, (movie_id,))
        return render_template('edit_movie.html', movie=movie[0])
    elif request.method == 'POST':
        link = request.form['link']
        photo = request.form['photo']
        cName = request.form['cName']
        introduce = request.form['introduce']
        grade = request.form['grade']
        evaluatorsNum = request.form['evaluatorsNum']
        ink = request.form['ink']
        sql = "UPDATE movieFriends.movies SET link = %s, photo = %s, cName = %s, introduce = %s, " \
              "grade = %s, evaluatorsNum = %s, ink = %s WHERE id = %s "
        db.execute(sql, (link, photo, cName, introduce, grade, evaluatorsNum, ink, movie_id))
        return redirect(url_for('main.manage_movie'))


# 删除电影逻辑
@bp.route('/delete_movie/<int:movie_id>', methods=['GET', 'POST'])
def delete_movie(movie_id):
    sql = "DELETE FROM movieFriends.movies WHERE id = %s"
    db.execute(sql, (movie_id,))
    return redirect(url_for('main.manage_movie'))


# 删除用户逻辑
@bp.route('/delete_user', methods=['DELETE'])
def delete_user():
    if request.method == 'DELETE':
        username = request.args.get('username')
        sql = 'DELETE FROM movieFriends.user WHERE username = %s'
        db.execute(sql, (username,))
        session.pop(username)
        return jsonify({'message': '删除成功'}), 200
    else:
        return jsonify({'error': '操作失败'}), 405
